package ${appPkg}.web.controller.${controllerPkg};

import javax.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.StrUtil;
import cn.com.dhcc.app.core.util.UUIDGenerator;
import cn.com.dhcc.app.core.web.controller.BaseController;
import ${appPkg}.entity.${entityPkg}.${beanName};
import ${appPkg}.service.${servicePkg}.${beanName}Service;

@Controller
@RequestMapping("/${moduleUri}")
public class ${beanName}Controller extends BaseController {

	private static Logger logger = Log.getExistLogger(LOGTYPE.SAI);

	@Autowired
	private ${beanName}Service ${beanName?uncap_first}Service;

	@RequestMapping("/${beanUri}_list")
	public String list(Model model, Pager pager,HttpServletRequest request) {
		try {
			String order = request.getParameter("order");
			String orderName = request.getParameter("orderName");
			WhereCondition searchData = new WhereCondition();
			<#if (viewColList?size>0) >
			searchData.where();
			</#if>
			<#list viewColList as vo>
			<#if vo.search>
			String ${vo.name} = request.getParameter("${vo.name}");
			if(StrUtil.isNotBlank(${vo.name})){
				searchData.and().like("${vo.name}", "%" + ${vo.name} + "%");
			}
			</#if>
			</#list>
			if (StrUtil.isNotBlank(orderName)) {
				searchData.orderBy(orderName + " " + order);
			} else {
				searchData.orderBy("created  desc");
			}
			PagerData<${beanName}> datas = ${beanName?uncap_first}Service.getPagerList(pager, searchData);
			model.addAttribute(DATA_KEY, datas.getDatas());
			setTotoal(model, datas.getTotal());
			return "${viewPkg}/${beanUri}_list";
		} catch (Exception e) {
			logger.error("操作失败", e);
			return toAlertPage(model, e.getMessage());
		}
	}
	
	
	
	@RequestMapping("/${beanUri}_get")
	public String get(Model model, String id) {
		try {
			${beanName} cat = ${beanName?uncap_first}Service.get(id);
			model.addAttribute(VO_KEY, cat);
			return "${viewPkg}/${beanUri}_get";
		} catch (Exception e) {
			logger.error("操作失败", e);
			return toAlertPage(model, e.getMessage());
		}
	}
	

	@RequestMapping("/${beanUri}_add")
	public String add(Model model) {
		try {
			return "${viewPkg}/${beanUri}_add";
		} catch (Exception e) {
			logger.error("操作失败", e);
			return toAlertPage(model, e.getMessage());
		}
	}

	@RequestMapping(value = "/add_${beanUri}", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(Model model, ${beanName} vo) {
		String msg = "";
		try {
			vo.setId(UUIDGenerator.getUUID());
			vo.setCreated(new Date());
			${beanName?uncap_first}Service.insert(vo);
			msg = "添加成功";
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "删除失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}

	@RequestMapping("/${beanUri}_update")
	public String update(Model model, String id) {
		try {
			${beanName} cat = ${beanName?uncap_first}Service.get(id);
			model.addAttribute(VO_KEY, cat);
			return "${viewPkg}/${beanUri}_update";
		} catch (Exception e) {
			logger.error("操作失败", e);
			return toAlertPage(model, e.getMessage());
		}
	}

	@RequestMapping(value = "/update_${beanUri}", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(Model model, ${beanName} vo) {
		String msg = "";
		try {
			vo.setModified(new Date());
			${beanName?uncap_first}Service.updateIgnoreNull(vo);
			msg = "更新成功";
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "更新失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}

	/**
	 * 根据ID删除记录
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/${beanUri}_delete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(Model model, String id) {
		try {
			${beanName?uncap_first}Service.delete(id);
			return jsonSuccessResult("删除成功");
		} catch (Exception e) {
			logger.error("删除失败", e);
			return jsonFailResult("删除失败");
		}
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/${beanUri}_deletes", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteByIds(@RequestParam("ids") String[] ids) {
		String msg = "";
		try {
			msg = "删除成功";
			${beanName?uncap_first}Service.deleteBatchById(ids);
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "批量删除失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}
	
	<#list viewColList as vo>
	<#if vo.ajaxCheck>
	/**
	 * 表单ajax校验
	 * @param name
	 * @param oldValue
	 * @return
	 */
	@RequestMapping("/${beanUri}_${vo.name}")
	@ResponseBody
	public String check${vo.name?capitalize}(String ${vo.name}, String oldValue) {
		try {
			if (${beanName?uncap_first}Service.isPropertyUnique("${vo.name}", ${vo.name}, oldValue)) {
				return FORM_CHECK_SUCCESS;
			} else {
				return FORM_CHECK_FAIL;
			}
		} catch (Exception e) {
			return FORM_CHECK_FAIL;
		}
	}
	</#if>
	</#list>
}

